package com.fowo.api.sys.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fowo.api.sys.entity.SysLoginLog;
import com.fowo.api.sys.mapper.SysLoginLogMapper;
import com.fowo.api.sys.model.SysLoginLogItemVo;
import com.fowo.api.sys.model.SysLoginLogSearch;
import com.fowo.api.sys.service.SysLoginLogService;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public Page<SysLoginLogItemVo> searchPage(SysLoginLogSearch search) {
        return baseMapper.searchPage(search.toPage(), search);
    }

    @Override
    public List<SysLoginLogItemVo> search(SysLoginLogSearch search) {
        return baseMapper.search(search);
    }

    @Override
    public boolean create(SysLoginLog log) {
        return save(log);
    }

    @Transactional
    @Override
    public boolean clean() {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo == null) {
            return false;
        }
        baseMapper.clean();
        SysLoginLog sysLoginLog = SysLoginLog.createWithUserId(jwtUserInfo.getUserId());
        return create(sysLoginLog);
    }
}
